package com.boxin.ims.modules.wechat.busi.game;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.ims.modules.wechat.busi.BusinessProcessor;
import com.boxin.ims.modules.wechat.busi.ProcessMessage;
import com.boxin.ims.modules.wechat.entity.Option;
import com.boxin.ims.modules.wechat.entity.Question;
import com.boxin.ims.modules.wechat.entity.Questionnaire;
import com.boxin.ims.modules.wechat.entity.QuestionnaireReplyHuman;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.QuestionService;
import com.boxin.ims.modules.wechat.service.QuestionnaireService;

/**
 * 
 * @author Administrator
 * @date 2014-8-22
 * @function <p> 问题抢答游戏 </p>
 */
@Service
public class QuestionProcessor extends BusinessProcessor{

	
	@Autowired
	QuestionnaireService questionnaireService;
	
	
	@Autowired
	QuestionService questionService;
	
	@Override
	public ProcessMessage process() {
		
		WechatMember member= memberCheckAndReply();
		
		if(member!=null){
			Questionnaire questionnaire=new Questionnaire();
			questionnaire.setUser(user);
			
			List<Questionnaire> questionnaireList = questionnaireService.findByStartup(questionnaire);
			
			if(questionnaireList.size()==0){
				writeToUser("抢答还未开启，您准备好了没!!");
				return null;
			}
			
			
			List<Question> questionList=questionService.findByQuestionnaireId(questionnaireList.get(0).getId());
			StringBuffer desc=new StringBuffer();
			for (int i = 0; i < questionList.size(); i++) {
				if(questionList.get(i).getType().equals("0")){
					desc.append((i+1)+".");
					desc.append(questionList.get(i).getQuestion())
					.append("\n");
					for (Option option : questionList.get(i).getOption()) {  
						desc.append(option.getName()).append("\t\t");
					}  
					desc.append("\n\n");
				}
			}
			
			writeImageToUser("抢答", desc.toString(), "", "");
			
		}
		
//		
//		
//		model.addAttribute("questionList", questionList);
//		model.addAttribute("questionnaire", questionnaire);
//		//一战到底需要wechatId
//		if("1".equals(questionnaire.getType())){
//			String wechatId=request.getParameter("wechatId");
//			model.addAttribute("wechatId", wechatId);
//			WechatMember member= wechatMemberService.getByWechatMemberId(questionnaire.getUser(), wechatId);
//			if(questionnaire.getActivityConfig().getLotteryNumber()<=0){
//				return "redirect:/f/wechat/questionnaireReply/questionnaireResult?result=1";
//			}
//			if(member==null){
//				String str="2";
//				return "redirect:/f/wechat/questionnaireReply/questionnaireResult?result="+str;
//			}
//			if("0".equals(questionnaire.getIsRepeat())){
//				
//				List<QuestionnaireReplyHuman> humanList= questionnaireReplyHumanService.getByWechatId(member.getId(), questionnaire.getId());
//				if(humanList.size()>0){
//					return "redirect:/f/wechat/questionnaireReply/questionnaireResult?result=4";
//				}
//			}
//		}
//		if("2".equals(questionnaire.getType())){
//			return "modules/member/themes/ShiShangHong/voterList";
//		}
//		return "modules/member/themes/ShiShangHong/viewQuestionnaire";
//		
//		
		
		return null;
	}

	@Override
	public boolean isNeedBeMember() {
		
		return false;
	}

}
